package com.ccx.demo.config;

import org.ehcache.PersistentCacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.redisson.spring.cache.CacheConfig;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 当前模块缓存配置
 *
 * @author 谢长春 2020-03-20
 */
@Component
public class DemoServiceCacheConfig implements CacheConfiguration.ICacheConfig {
    @Override
    public CacheManagerBuilder<PersistentCacheManager> join(CacheManagerBuilder<PersistentCacheManager> cacheManagerBuilder) {
        return cacheManagerBuilder
//                .withCache(IEntityCache.CACHE_ROW_BY_ID, CacheConfigurationBuilder
//                        .newCacheConfigurationBuilder(KeyType.class, Entity.class, ResourcePoolsBuilder.newResourcePoolsBuilder()
//                                .heap(10, EntryUnit.ENTRIES)
//                                .offheap(1, MemoryUnit.MB)
//                                .disk(100, MemoryUnit.MB, true)
//                        )
//                       .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofDays(30)))
//                       .withValueSerializer(new FastJsonEhcacheSerializer<>(Entity.class))
//                )
                ;
    }

    @Override
    public Map<String, CacheConfig> join() {
        return new HashMap<String, CacheConfig>(1) {{
//            put(IEntityCache.CACHE_ROW_BY_ID, new CacheConfig(30, 15));
        }};
    }

    @Override
    public void join(RedisCacheManager.RedisCacheManagerBuilder cacheManagerBuilder) {
//        cacheManagerBuilder
//                .withCacheConfiguration(IEntityCache.CACHE_ROW_BY_ID, RedisCacheConfiguration.defaultCacheConfig()
//                        .entryTtl(Duration.ofDays(30)) // 设置过期时间
//                        .prefixCacheNameWith(IEntityCache.CACHE_ROW_BY_ID.concat(":")) // 设置缓存前缀
//                        .disableCachingNullValues() // 禁止缓存 null 值
//                        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new CacheConfig.CustomFastJsonRedisSerializer<>(Entity.class)))
//                )
//        ;
    }

//    @Override
//    public Map<String, CacheConfig> join() {
////        return new HashMap<String, CacheConfig>() {{
////            put(IEntityCache.CACHE_ROW_BY_ID, new CacheConfig(Duration.ofDays(30).toMillis(), 0L));
////        }};
//        return new HashMap<>();
//    }
}
